package com.test30_IO流;

import org.junit.Test;

import java.io.*;

/**
 * 内存操作流
 * 字节内存操作流：ByteArrayInputStream、ByteArrayOutputStream
 * 字符内存操作流：CharArrayReader、CharArrayWriter
 */
public class Demo09_ByteArrayInputStream {

    /**
     * 基于内存的大小写转换
     */
    @Test
    public void test1() throws Exception {
        String message = "www.YOOTK.com";
        InputStream input = new ByteArrayInputStream(message.getBytes());
        OutputStream output = new ByteArrayOutputStream();
        int data = 0;   // 每次读取单个字节
        while ((data = input.read()) != -1) {
            output.write(Character.toLowerCase(data));
        }
        System.out.println(output);
        input.close();
        output.close();
    }

    /**
     * 实现文件内容的全部读取
     */
    @Test
    public void test2() throws Exception {
        File file = new File("F:" + File.separator + "muyan" + File.separator + "message.txt");
        if(file.exists()) {
            InputStream input = new FileInputStream(file);
            ByteArrayOutputStream output = new ByteArrayOutputStream(); // 不要转型
            byte[] data = new byte[10];
            int len = 0;
            while ((len = input.read(data)) != -1) {
                output.write(data, 0, len);
            }
            String content = new String(output.toByteArray());  // 字节数组转换为字符串
            System.out.println(content);
            input.close();
            output.close();
        }
    }
}
